//package com.elasticsearch.demo.service;
//
//import org.elasticsearch.action.bulk.BulkRequest;
//import org.elasticsearch.action.bulk.BulkResponse;
//import org.elasticsearch.action.delete.DeleteRequest;
//import org.elasticsearch.action.index.IndexRequest;
//import org.elasticsearch.action.index.IndexResponse;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchRequestBuilder;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.action.update.UpdateRequest;
//import org.elasticsearch.action.update.UpdateResponse;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.client.core.CountRequest;
//import org.elasticsearch.client.core.CountResponse;
//import org.elasticsearch.common.geo.GeoPoint;
//import org.elasticsearch.common.unit.TimeValue;
//import org.elasticsearch.common.xcontent.XContentType;
//import org.elasticsearch.index.query.*;
//import org.elasticsearch.index.reindex.DeleteByQueryRequest;
//import org.elasticsearch.index.reindex.UpdateByQueryRequest;
//import org.elasticsearch.script.Script;
//import org.elasticsearch.search.SearchHit;
//import org.elasticsearch.search.SearchHits;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//
//@Service
//public class EsRestService {
//    @Autowired
//    RestHighLevelClient client;
//
//
//    /**
//     * @return
//     * @Author xieshanzhong
//     * @Description 创建文档
//     * @Date 11:57 2022/3/17
//     * @Param
//     **/
//    public void createIndex() {
//        IndexRequest indexRequest = new IndexRequest(
//                "posts", //index name
//                "doc",  // type
//                "1");   // doc id
//        String jsonString = "{" +
//                "\"user\":\"kimchy\"," +
//                "\"postDate\":\"2013-01-30\"," +
//                "\"message\":\"trying out Elasticsearch\"" +
//                "}";
//        indexRequest.source(jsonString, XContentType.JSON);
//        try {
//            IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
//            String index = indexResponse.getIndex();
//            String id = indexResponse.getId();
//            Long version = indexResponse.getVersion();
//            System.out.println("index=" + index + "id=" + id + "version=" + version);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    //文档操作
//
//    /**
//     * @return
//     * @Author xieshanzhong
//     * @Description 单条记录写入
//     * @Date 15:36 2022/1/6
//     * @Param
//     **/
//    public void singleIndexDoc(Map<String, Object> map, String indexName, String indexId) {
//        IndexRequest indexRequest = new IndexRequest(indexName).id(indexId).source(map);
//        try {
//            IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
//            String index = indexResponse.getIndex();
//            String id = indexResponse.getId();
//            Long version = indexResponse.getVersion();
//            System.out.println("index=" + index + ",id=" + id + ",version=" + version);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    /**
//     * @return
//     * @Author xieshanzhong
//     * @Description 批量记录写入
//     * @Date 16:33 2022/1/6
//     * @Param
//     **/
//    public void bulkIndexDoc(String indexName, String _id, List<Map<String, Object>> list) {
//        // 批量操作BulkRequest对象
//        BulkRequest bulkRequest = new BulkRequest(indexName);
//        for (Map<String, Object> stringObjectMap : list) {
//            String docId = stringObjectMap.get(_id).toString();
//            IndexRequest indexRequest = new IndexRequest().source(stringObjectMap);
//            bulkRequest.add(indexRequest);
//        }
//        bulkRequest.timeout(TimeValue.timeValueSeconds(5));
//        RequestOptions options;
//        try {
//            // 执行批量记录写入
//            BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
//            if (bulkResponse.hasFailures()) {
//                System.out.println("bulk fail" + bulkResponse.buildFailureMessage());
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    /**
//     * @return
//     * @Author xieshanzhong
//     * @Description 单条更新记录
//     * @Date 21:25 2022/1/8
//     * @Param
//     **/
//    public void singleUpdate(String indexName, String docIdKey, Map<String, Object> map) {
//        UpdateRequest updateRequest = new UpdateRequest(indexName, docIdKey);
//        updateRequest.doc(map);
//        try {
//            UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
//            String index = updateResponse.getIndex();
//            String id = updateResponse.getId();
//            System.out.println("index:=" + index);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//    }
//
//
//    /**
//     * @return
//     * @Author xieshanzhong
//     * @Description 批量更新文档操作
//     * @Date 16:54 2022/1/9
//     * @Param
//     **/
//    public void bulkUpdateDoc(String indexName, String docIdKey, List<Map<String, Object>> list) {
//        // 批量操作BulkRequest对象
//        BulkRequest bulkRequest = new BulkRequest();
//        for (Map<String, Object> stringObjectMap : list) {
//            String docId = stringObjectMap.get(docIdKey).toString();
//            stringObjectMap.remove(docId);
//            // 创建UpdateRequest对象
//            bulkRequest.add(new UpdateRequest(indexName, docId).doc(stringObjectMap));
//        }
//        try {
//            // 执行批量记录写入
//            BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
//            if (bulkResponse.hasFailures()) {
//                System.out.println("bulk fail" + bulkResponse.buildFailureMessage());
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    /**
//     * @return
//     * @Author xieshanzhong
//     * @Description 根据条件更新查询
//     * @Date 17:04 2022/1/9
//     * @Param
//     **/
//    public void updateCityByQuery(String index, String oldCity, String newCity) {
//        SearchRequestBuilder searchRequestBuilder;
//        UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest(index);
//        updateByQueryRequest.setQuery(new TermQueryBuilder("city", oldCity));
//        updateByQueryRequest.setScript(new Script("ctx._source['city']=" + newCity + ",:"));
//        try {
//            client.updateByQuery(updateByQueryRequest, RequestOptions.DEFAULT);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//    }
//
//    /**
//     * @return
//     * @Author xieshanzhong
//     * @Description 删除记录
//     * @Date 17:13 2022/1/9
//     * @Param
//     **/
//    public void deleteDoc(String index, String docId) {
//        DeleteRequest deleteRequest = new DeleteRequest(index, docId);
//        try {
//            client.delete(deleteRequest, RequestOptions.DEFAULT);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    /**
//     * @return
//     * @Author xieshanzhong
//     * @Description 根据条件删除
//     * @Date 20:28 2022/1/9
//     * @Param
//     **/
//    public void deleteByQuery(String index, String city) {
//        DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(index);
//        deleteByQueryRequest.setQuery(new TermQueryBuilder("city", city));
//        try {
//            client.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * @return
//     * @Author xieshanzhong
//     * @Description 获取数量
//     * @Date 20:44 2022/1/9
//     * @Param
//     **/
//    public long getCityCount() {
//        CountRequest countRequest = new CountRequest("hotel");
//        // 创建搜索builder
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        // 构建query
//        searchSourceBuilder.query(new TermsQueryBuilder("city", "深圳"));
//        countRequest.source(searchSourceBuilder);
//        try {
//            CountResponse countResponse = client.count(countRequest, RequestOptions.DEFAULT);
//            return countResponse.getCount();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return 0;
//    }
//
//
//    /**
//     * @return
//     * @Author xieshanzhong
//     * @Description 构建查询全部
//     * @Date 9:58 2022/1/10
//     * @Param
//     **/
//    public void matchAllSearch() {
//        // 构建一个SearchRequest
//        SearchRequest searchRequest = new SearchRequest("hotel");
//        // 构建一个SearchSourceBuilder
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery().boost(2.0f);
//        searchSourceBuilder.query(matchAllQueryBuilder);
//        searchRequest.source(searchSourceBuilder);
//        printResult(searchRequest);
//
//    }
//
//    private void printResult(SearchRequest searchRequest) {
//        RequestOptions options;
//        try {
//            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
//            SearchHits searchHits = searchResponse.getHits();
//            for (SearchHit searchHit : searchHits) {
//                System.out.println("index_id:=" + searchHit.getId());
//                System.out.println("source:=" + searchHit.getSourceAsMap());
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//    }
//
//    /**
//     * @return
//     * @Author xieshanzhong
//     * @Description
//     * @Date 10:22 2022/1/10
//     * @Param
//     **/
//    public void termDateSearch() {
//        // 构建一个SearchRequest
//        SearchRequest searchRequest = new SearchRequest("hotel");
//        // 构建一个SearchSourceBuilder
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        searchSourceBuilder.query(QueryBuilders.termQuery("create_time", "2021050916000"));
//        searchRequest.source(searchSourceBuilder);
//        printResult(searchRequest);
//
//    }
//
//
//    /**
//     * @return
//     * @Author xieshanzhong
//     * @Description 构建rangeSearch
//     * @Date 10:35 2022/1/10
//     * @Param
//     **/
//    public void rangeSearch() {
//        // 构建一个SearchRequest
//        SearchRequest searchRequest = new SearchRequest("hotel");
//        // 构建一个SearchSourceBuilder
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        // 构建range查询
//        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("create_time").gte("2021050916000").lte("2021050916999");
//        // 设置查询
//        searchSourceBuilder.query(rangeQueryBuilder);
//        searchRequest.source(searchSourceBuilder);
//        printResult(searchRequest);
//    }
//
//
//    public void mustSearch() {
//        SearchRequest searchRequest = new SearchRequest("hotel");
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        QueryBuilder queryBuilder;
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//        // 构建TermQuery
//        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("city", "深圳");
//        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("price").gte(200).lte(500);
//
//        boolQueryBuilder.must(termQueryBuilder).must(rangeQueryBuilder);
//        searchSourceBuilder.query(boolQueryBuilder);
//        searchRequest.source(searchSourceBuilder);
//        printResult(searchRequest);
//
//    }
//
//
//    /**
//     * @return
//     * @Author xieshanzhong
//     * @Description 忽略打分只要有包含即可
//     * @Date 19:09 2022/1/12
//     * @Param
//     **/
//    public void constantScoreSearch() {
//        SearchRequest searchRequest = new SearchRequest();
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        QueryBuilder filterBuilder;
//        ConstantScoreQueryBuilder constantScoreQueryBuilder = new ConstantScoreQueryBuilder(QueryBuilders.termQuery("city", "深圳"));
//        searchSourceBuilder.query(constantScoreQueryBuilder);
//        constantScoreQueryBuilder.boost(2.0f);
//        searchRequest.source(searchSourceBuilder);
//        printResult(searchRequest);
//    }
//
//
//    /**
//     * @return
//     * @Author xieshanzhong
//     * @Description 根据多边形的坐标点查询
//     * @Date 19:48 2022/1/12
//     * @Param
//     **/
//    public void geoPolySearch() {
//        SearchRequest searchRequest = new SearchRequest("hotel");
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        List<GeoPoint> geoPoints = new ArrayList<>();
//        geoPoints.add(new GeoPoint(39.9999, 129.99999));
//        geoPoints.add(new GeoPoint(49.9999, 129.99999));
//        geoPoints.add(new GeoPoint(59.9999, 129.99999));
//        searchSourceBuilder.query(QueryBuilders.geoPolygonQuery("location", geoPoints));
//        searchRequest.source(searchSourceBuilder);
//        printResult(searchRequest);
//
//    }
//}
//
//
//
//
//
//
//
//
//
